<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加产品</title>
    <script src="./lib/vue.min.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="./lib/elementui-index.js"></script>
    <script src="./lib/axios.min.js"></script>
    <script src="./lib/token.js"></script>
</head>
<body>
    <div id="app">
        <el-form label-width="80px">
        <div v-for="i in arr.length">
            <div>第{{i}}个产品</div>
                <el-form-item label="产品型号">
                  <el-input v-model="arr[i-1].ptype"></el-input>
                </el-form-item>
    
                  <el-form-item label="材质要求">
                    <el-select v-model="arr[i-1].prequire">
                      <el-option label="33#WSY" value="33#WSY"></el-option>
                      <el-option label="41#WSY" value="41#WSY"></el-option>
                      <el-option label="33#PTY" value="33#PTY"></el-option>
                    </el-select>
                  </el-form-item>
    
                <el-form-item label="尺寸要求">
                    <el-input v-model="arr[i-1].psize"></el-input>
                </el-form-item>
                <el-form-item label="数量"> 
                    <el-input v-model="arr[i-1].pcount" type="number"></el-input>
                </el-form-item>
        </div>
        

            <el-form-item>
                <template>
                    <el-button type="primary" @click="open">添加</el-button>
                </template>
              <el-button type="primary" @click="queryOrder">订单查询</el-button>
              <el-button type="primary" @click="addcount">+</el-button>
              <el-button type="primary" @click="subcount">-</el-button>
            </el-form-item>
          </el-form>
    </div>


    <script>
        new Vue({
            el:"#app",
            data() {
                return {
                    form: {
                        ooid:'',
                        ptype: '',
                        prequire:'33#WSY',
                        psize:'±0.5',
                        pcount:'',
                    },
                    arr:[{
                        ooid:'',
                        ptype: '',
                        prequire:'33#WSY',
                        psize:'±0.5',
                        pcount:'',
                    }],
                    count:1
                }
            },
            methods: {
                addProduct(){
                    console.log(this.arr)
                    axios.post("http://localhost:8001/production/product/add",this.arr)
                    .then(res=>{
                        console.log(res.data)
                        if(res.data.code === 20000){
                            this.$message.success("添加成功")
                        }else{
                            this.$message.error("添加失败")
                        }
                    })
                },
                open() {
                    this.$confirm('是否添加订单?', '提示', {
                    confirmButtonText: '添加',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(() => {
                        this.addProduct()
                        //location.href = "addproduct.html"
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消'
                        });        
                    });
                },
                queryOrder(){
                    sessionStorage.ooid = '';
                    location.href = "order.html"
                },
                subcount(){
                    if(this.arr.length > 1){
                        this.arr.pop()
                    }
                },
                addcount(){
                    this.arr.push(JSON.parse(JSON.stringify(this.form)))
                }
            },
            created(){
                this.arr[0].ooid = sessionStorage.ooid
                console.log(this.arr[0].ooid)
                if(this.arr[0].ooid === '' || this.arr[0].ooid === undefined || this.arr[0].ooid === null){
                    location.href = 'addOrder.html'
                }
            }
        })
    </script>
</body>
</html>